/*This file is part of PaperBall.

PaperBall is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PaperBall is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PaperBall.  If not, see <http://www.gnu.org/licenses/>.*/

package com.ran3000.paperball;

import javax.microedition.khronos.opengles.GL10;

import com.ran3000.paperball.framework.gl.Animation;
import com.ran3000.paperball.framework.gl.Camera2D;
import com.ran3000.paperball.framework.gl.SpriteBatcher;
import com.ran3000.paperball.framework.gl.TextureRegion;
import com.ran3000.paperball.framework.impl.GLGraphics;

public class WorldRenderer {
	static final float FRUSTUM_WIDTH = 15;
	static final float FRUSTUM_HEIGHT = 10;
	GLGraphics glGraphics;
	World world;
	Camera2D cam;
	SpriteBatcher batcher;
	float lastX;
	float angle;
	
	
	public WorldRenderer(GLGraphics glGraphics, SpriteBatcher batcher, World world) {
		this.glGraphics = glGraphics;
		this.world = world;
		this.cam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
		this.batcher = batcher;

		lastX = 0;
		angle = 0;
	}
	
	public void render() {
		
		cam.setViewportAndMatrices();
		
		GL10 gl = glGraphics.getGL();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		batcher.beginBatch(Assets.items);
		batcher.drawSprite(7.5f, 5, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.background);
		renderBombs();
		renderBall();
		renderExplosions();
		renderShield();
		batcher.endBatch();
		
		gl.glDisable(GL10.GL_BLEND);
	}
	
	
	private void renderBall() {
		Ball ball = world.ball;
		
		if(ball.position.x > lastX) {
			angle -= Math.abs(ball.velocity.x);
			lastX = ball.position.x;
		}
		
		if(ball.position.x < lastX) {
			angle += Math.abs(ball.velocity.x);
			lastX = ball.position.x;
		}
		
		batcher.drawSprite(ball.position.x, ball.position.y, 2.25f, 2.25f, angle, Assets.paperBall);
	}
	
	private void renderBombs() {
		int len = world.bombs.size();
		for(int i = 0; i < len; i++) {
			Bomb bomb = world.bombs.get(i);
			TextureRegion keyFrame = Assets.bomb.getKeyFrame(bomb.stateTime, Animation.ANIMATION_LOOPING);
			batcher.drawSprite(bomb.position.x, bomb.position.y + 0.375f, 1.5f, 2.25f, keyFrame);
		}
	}
	
	private void renderExplosions() {
		int len = world.explosions.size();
		for(int i = 0; i < len; i++) {
			Explosion explosion = world.explosions.get(i);
			TextureRegion keyFrame = Assets.explosion.getKeyFrame(explosion.stateTime, Animation.ANIMATION_NONLOOPING);
			batcher.drawSprite(explosion.position.x, explosion.position.y, 2.25f, 2.25f, keyFrame);
		}
	}
	
	private void renderShield() {
		for(int i = 0; i < world.shields.size(); i++){
			Shield shield = world.shields.get(i);
			batcher.drawSprite(shield.position.x, Shield.SHIELD_Y, Shield.SHIELD_WIDTH, Shield.SHIELD_HEIGHT, Assets.shield);
		}
	}
}
